#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<omp.h>

double start, end;

#define NUM_THREADS 2

void main(){
    int n = 10000;
    double average;
    double sum;
    double test[10000];
    start = omp_get_wtime();
    omp_set_num_threads(NUM_THREADS);
    
    // 为数组提供数据
    for (int i = 0; i <= n; i++)
    {
        test[i] = rand();
    }

    #pragma omp parallel
    {
    // max and min
    double max = test[0];
    double min = test[0];
    #pragma omp parallel for reduction(max:max)
    for (int i = 1; i < n; i++)
    {
        if (max <= test[i])
        {
            
            max = test[i];
        }

    #pragma omp parallel for reduction(min:min)    
    for (int j = 1; j < n; j++)    
        if (min >= test[j])
        {
            min = test[j];
        }
        
    }
    //printf("max:%f\nmin:%f\n",max,min);
    
    
    //average
    #pragma omp for reduction(+:sum)
    for (int k = 0; k < n; k++)
    {
        sum += test[k];
    }

    average = sum/n;
    //printf("average:%f\n",average);

    //std
    double std = 0.0;
    double total = 0.0;
    #pragma omp for reduction(+:sum)
    for (int m = 0; m < n; m++)
    {
        total += (test[m] - average)*(test[m] - average);
    }
    
    std = sqrt((double)(total/n)); //没有math库 解决方法：终端输入 gcc file -lm
    //printf("std:%f",std);
    }
    
    end = omp_get_wtime();
    printf("time:%f", end - start);
}